package com.tanhua.app.controller;

import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.app.service.UsersService;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    //用户资料 - 读取
    //GET /users
    @GetMapping
    //public ResponseEntity findUserInfo(Long userID,
    //                                   @RequestHeader("Authorization") String token) {
    public ResponseEntity findUserInfo(Long userID) {
        ////1 校验用户是否登录
        //boolean verifyToken = JwtUtils.verifyToken(token);
        //if (!verifyToken) {
        //    return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        //}
        //
        ////2 解析Token  获取用户主键id
        //Claims claims = JwtUtils.getClaims(token);
        //Long id = Long.parseLong(claims.get("id").toString());

        if (userID == null) {
            userID = UserHolder.getUserId();
        }

        //3 调用Service实现功能
        UserInfoVo vo = usersService.findUserInfo(userID);

        //4 返回结果
        return ResponseEntity.ok(vo);
    }

    //用户资料 - 修改
    //PUT /users
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,
                                         @RequestHeader("Authorization") String token) {
        //1 校验用户是否登录
        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }

        //2 解析Token  获取用户主键id
        Claims claims = JwtUtils.getClaims(token);
        Long id = Long.parseLong(claims.get("id").toString());

        //3 把id设置到UserInfo中
        userInfo.setId(id);

        //4 调用Service实现业务逻辑
        usersService.updateUserInfo(userInfo);

        //5 返回结果
        return ResponseEntity.ok(null);
    }

    //用户通用设置 - 读取
    //GET /users/settings
    @GetMapping("/settings")
    public ResponseEntity findSettings() {
        //1. 调用Service实现业务逻辑
        SettingsVo vo = usersService.findSettings();

        //2. 返回结果
        return ResponseEntity.ok(vo);
    }

    //设置陌生人问题 - 保存
    //POST /users/questions
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        //1 获取参数  就是问题
        String content = (String) map.get("content");

        //1. 调用Service实现业务逻辑
        usersService.questions(content);

        //2. 返回结果
        return ResponseEntity.ok(null);
    }

    //通知设置 - 保存
    //POST /users/notifications/setting
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map param) {
        //1. 调用Service实现业务逻辑
        usersService.notifications(param);

        //2. 返回结果
        return ResponseEntity.ok(null);
    }

    //黑名单 - 翻页列表
    //GET /users/blacklist
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        //1. 调用Service实现业务逻辑
        PageResult pageResult = usersService.blacklist(page, pagesize);

        //2. 返回结果
        return ResponseEntity.ok(pageResult);
    }

    //黑名单 - 移除
    //DELETE /users/blacklist/:uid
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlacklist(@PathVariable("uid") Long uid) {
        //1. 调用Service实现业务逻辑
        usersService.deleteBlacklist(uid);

        //2. 返回结果
        return ResponseEntity.ok(null);
    }


    //互相喜欢、喜欢、粉丝
    //GET /users/friends/:type
    @GetMapping("/friends/{type}")
    public ResponseEntity<PageResult> friends(@RequestParam(defaultValue = "1") Integer page,
                                              @RequestParam(defaultValue = "10") Integer pagesize,
                                              @PathVariable("type") Integer type) {
        PageResult pageResult = null;
        if(type == 1){
            //互相喜欢
            pageResult = usersService.eachLove(type,page,pagesize);

        } else if (type == 2){
            pageResult = usersService.love(type,page,pagesize);
        } else if (type == 3){
            pageResult = usersService.fan(type,page,pagesize);
        } else if(type == 4){}
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     * GET /users/counts
     * 请求参数：
     * null
     * 响应结果：
     * eachLoveCount  互相喜欢  loveCount 喜欢 fanCount 粉丝 Integer 类型
     * 可以使用Map 集合来封装
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        //1.用户id
        Long userId = UserHolder.getUserId();
        //2.调用service层 进行逻辑处理
        Map<String,Integer> map = usersService.counts(userId);
        //3.返回
        return ResponseEntity.ok(map);
    }

    /**
     * 粉丝 - 喜欢
     * POST  /users/fans/:uid
     * 请求参数：
     * 路径参数： uid  喜欢用户的id
     * 响应结果：
     * null
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity saveLove(@PathVariable("uid") Long likeUserId) {
        Long userId = UserHolder.getUserId();
        usersService.saveLove(userId, likeUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 粉丝 - 取消喜欢
     * DELETE  /users/like/:uid
     * 请求参数：
     * 路径参数： uid  取消喜欢用户的id
     * 响应结果：
     * null
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity deleteLove(@PathVariable("uid") Long likeUserId) {
        Long userId = UserHolder.getUserId();
        usersService.deleteLove(userId, likeUserId);
        return ResponseEntity.ok(null);
    }
}
